package com.example.demotest.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demotest.entity.Aa;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.*;

public class LogReader implements Runnable {
   private File logFile = null;
   private long lastTimeFileSize = 0; // 上次文件大小
    private List<Map<String,String>> mapList = new ArrayList<>(); // 上次文件大小
   private static SimpleDateFormat dateFormat = new SimpleDateFormat(
           "yyyy-MM-dd HH:mm:ss");
 
   public LogReader(File logFile) {
       this.logFile = logFile;
       lastTimeFileSize = logFile.length();
   }
 
   /**
    * 实时输出日志信息
    */
   @Override
   public void run() {
       while (true) {
           try {
               RandomAccessFile randomFile = new RandomAccessFile(logFile, "r");
               randomFile.seek(lastTimeFileSize);
               String tmp = null;
               Map<String,String> map= new HashMap<>();
               while ((tmp = randomFile.readLine()) != null) {
                   boolean contains = tmp.contains("=");
//                   System.out.println(dateFormat.format(new Date()) + "\t"
//                           + tmp);
//                   System.out.println(tmp.length());
                   if(contains){
                       String[] split = tmp.replaceAll("-","").split("=");
                       map.put(split[0].replaceAll(" ",""),split[1].replaceAll(" ",""));
                   }
//                   System.out.println(map);


//                   System.out.println(dateFormat.format(new Date()) + "\t"
//                           + tmp);

                   if(tmp.length()==0){
                           System.out.println("wwwwwwwwww");
                           mapList.add(map);
//                       Aa aa = JSON.parseObject(JSON.toJSONString(map), Aa.class);
//                       System.out.println(" map:{}"+map);
//                       System.out.println(" aa:{}"+aa);
//                       System.out.println("mapList.size()"+mapList.size());
//                        mapList=new ArrayList<>();
                   }
                   //构建对象
               }
//               System.out.println(tmp.length());
               if(null==tmp){

                   if(!map.isEmpty()){

                       Aa aa1=new Aa();
                       mapList.add(map);
//                       BeanUtils.populate(aa1 , map);
                       //批量insert
                       String s = JSONObject.toJSONString(map);
                       System.out.println(" s:{}"+s);
                       Aa aa = JSON.parseObject(JSONObject.toJSONString(map), Aa.class);
                       System.out.println(" map:{}"+map);
                       System.out.println(" aa:{}"+aa);
                       System.out.println("mapList.size()"+mapList.size());
                       System.out.println("批量insert");
                       System.out.println("mapList.size()"+mapList.size());
                       mapList.add(map);
                       System.out.println(mapList.size());
                   }

//                        mapList=new ArrayList<>();
               }
               lastTimeFileSize = randomFile.length();
               System.out.println(lastTimeFileSize);
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
           try {
               Thread.sleep(500);
           } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
   }


    public static void main(String[] args) {
        Map<String,String> obj=new HashMap<>();
        obj.put("AcctStatusType","111");

//        String s = JSONObject.toJSONString(obj);
        String s="{\"\\tacctStatusType\":\"Start\",\"\\tClass\":\"0x3438\",\"\\tUserName\":\"\\\"20000677\\\"\",\"\\tTmpString9\":\"\\\"ai:\\\"\",\"\\tFramedIPAddress\":\"10.103.154.68\",\"\\tCallingStationId\":\"\\\"6A797C5B02A6\\\"\",\"\\tEventTimestamp\":\"\\\"Nov23202200:00:00CST\\\"\",\"\\tAcctUniqueSessionId\":\"\\\"c2f094e4e8bcf0c317354a71de5aed0f\\\"\",\"\\tNASIPAddress\":\"172.20.4.252\",\"\\tAcctSessionId\":\"\\\"000000042022112216000002db834008100495\\\"\",\"\\tTimestamp\":\"1669132800\"}";
        System.out.println(s);
        Aa userTest = JSONObject.parseObject(JSONObject.toJSONString(obj), Aa.class);
        System.out.println(userTest);
    }
}